using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Peradigm.Framework.UI.Web;
using Liberty.Application.CaseManagement;
using Liberty.Application.ServiceMemberManagement;
using Liberty.Domain.ServiceMemberManagement;
using Liberty.UI.Service.ServiceMemberManagement.Controls;

namespace Liberty.UI.Service.ServiceMemberManagement
{
    [Serializable]
    public partial class ServiceMemberSearchView : AbstractPage
    {
        #region Constants
        private const string
            PAGE_GRANT_VIEW = "../CaseManagement/CaseView.aspx",
            PAGE_SERVICE_MEMBER_VIEW = "../ServiceMemberManagement/ServiceMemberView.aspx",
            PAGE_GRANT_LIST_VIEW ="../GrantManagement/GrantListView.aspx",
            ADD_CASE_RELATIVE_URL = "~/GrantApplicationWizardView.aspx?case=true",
            SESSION_SERVICE_MEMBERS_LIST = "ServiceMembersList";
        #endregion

        #region Event Handlers
        private void Page_Load( object sender, EventArgs e )
        {
            ucServiceMemberListView.Model = this.ServiceMembers;

            //AjaxControlToolkit Presentation
            // Register the search button on the search view as a trigger to the update panel.
            // Note that you must reference the script manager on the master page.
            ((Main)Page.Master).ScriptManager.RegisterAsyncPostBackControl( this.ucServiceMemberSearchView.FindControl( "cmdSearch" ) );
           
            this.ucServiceMemberListView.AddNewCaseEventListerners += new AddNewCaseEventHandler(OnAddNewCase);
        }

        private void OnServiceMemberSearchComplete( List<ServiceMember> serviceMembers )
        {
            this.ServiceMembers = serviceMembers;
            ucServiceMemberListView.Model = serviceMembers;
            ucServiceMemberListView.Visible = true;
        }

        private void OnAddNewCase(ServiceMember serviceMember)
        {
            ServiceMemberVariable serviceMemberVariable = new ServiceMemberVariable();
            serviceMemberVariable.ServiceMember = serviceMember;

            if (this.IsServiceMemberContext)
            {
                this.Response.Redirect(ADD_CASE_RELATIVE_URL);
            }
        }

        private void OnServiceMemberSelected( ServiceMember serviceMember )
        {
            ServiceMemberVariable serviceMemberVariable = new ServiceMemberVariable();
            serviceMemberVariable.ServiceMember = serviceMember;
            if( this.IsServiceMemberContext )
            {
                this.Response.Redirect( PAGE_SERVICE_MEMBER_VIEW );
            }
            else
            {
                this.Response.Redirect( PAGE_GRANT_VIEW );
            }
        }

        protected void btnAddGrant_Click( object sender, ImageClickEventArgs e )
        {
            this.Response.Redirect( PAGE_GRANT_LIST_VIEW );
        }
        #endregion

        #region Methods
        protected override void UpdateView()
        {
            base.UpdateView();
            ( (Main)this.Master ).HideAddGrantAction();
            ( (Main)this.Master ).HideServiceMembersAction();
        }
        #endregion

        #region Properties
        #endregion

        #region Private Methods
        protected override void InitializeHandlers()
        {
            base.InitializeHandlers();
            this.Load += new EventHandler( this.Page_Load );
            ucServiceMemberSearchView.SearchCompletedListeners += new ServiceMemberSearchCompleteEventHandler( this.OnServiceMemberSearchComplete );
            ucServiceMemberListView.ServiceMemberSelectedEventListerners += new ServiceMemberSelectedEventHandler( this.OnServiceMemberSelected );
            ucServiceMemberListView.AddNewCaseEventListerners += new AddNewCaseEventHandler( this.OnAddNewCase );
        }
        #endregion

        #region Private Properties

        private bool IsServiceMemberContext
        {
            get
            {
                return this.CurrentContextVariable.IsServiceMemberContext;
            }
        }

        private CurrentContextVariable CurrentContextVariable
        {
            get
            {
                if( i_CurrentContextVariable == null )
                {
                    i_CurrentContextVariable = new CurrentContextVariable();
                }
                return i_CurrentContextVariable;
            }
        }

        private List<ServiceMember> ServiceMembers
        {
            get
            {
                List<ServiceMember> serviceMembers = null;
                object serviceMembersSession = this.Session[SESSION_SERVICE_MEMBERS_LIST];
                if( serviceMembersSession != null )
                {
                    serviceMembers = (List<ServiceMember>)serviceMembersSession;
                }
                return serviceMembers;
            }
            set
            {
                this.Session[SESSION_SERVICE_MEMBERS_LIST] = value;
            }
        }
        #endregion

        #region Construction and Finalization
        public ServiceMemberSearchView()
        {
        }
        #endregion

        #region Data Elements
        private CurrentContextVariable i_CurrentContextVariable;
        #endregion
    }
}
